package com.dzl.goods.service.impl;

import com.dzl.base.service.BaseServiceImpl;
import com.dzl.config.exception.CommonException;
import com.dzl.constant.CommonConstants;
import com.dzl.goods.dao.WelfareGoodsLabelMapper;
import com.dzl.goods.pojo.WelfareGoodsLabel;
import com.dzl.goods.pojo.WelfareGoodsLabelRel;
import com.dzl.goods.pojo.query.WelfareGoodsLabelQuery;
import com.dzl.goods.pojo.vo.WelfareGoodsLabelVo;
import com.dzl.goods.service.WelfareGoodsLabelRelService;
import com.dzl.goods.service.WelfareGoodsLabelService;
import com.dzl.util.EmptyUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 商品标签表(WelfareGoodsLabel)表服务实现类
 * @author wangjian
 * @date 2021-08-02 09:34:23
 */
@Service
public class WelfareGoodsLabelServiceImpl extends BaseServiceImpl<WelfareGoodsLabel> implements WelfareGoodsLabelService {

    @Autowired
    private WelfareGoodsLabelMapper mapper;
    @Autowired
    private WelfareGoodsLabelRelService welfareGoodsLabelRelService;

    @Override
    public PageInfo<WelfareGoodsLabelVo> queryGoodsLabelListPage(WelfareGoodsLabelQuery query) {
        PageHelper.startPage(query);
        List<WelfareGoodsLabelVo> list = mapper.queryGoodsLabelListData(query);
        PageInfo<WelfareGoodsLabelVo> page = new PageInfo<WelfareGoodsLabelVo>(list);
        return page;
    }

    @Override
    public Integer updateGoodsLabel(WelfareGoodsLabel welfareGoodsLabel) {
        if (EmptyUtils.isEmpty(welfareGoodsLabel.getId())){
            mapper.insertSelective(welfareGoodsLabel);
        }else {
            mapper.updateByPrimaryKeySelective(welfareGoodsLabel);
        }
        return 1;
    }

    @Override
    public Integer deleteGoodsLabel(Integer labelId) {
        //查看标签是否关联了商品
        WelfareGoodsLabelRel welfareGoodsLabelRel = new WelfareGoodsLabelRel();
        welfareGoodsLabelRel.setLabelId(labelId);
       int count = welfareGoodsLabelRelService.selectCount(welfareGoodsLabelRel);
        if (count>0){
            throw new CommonException("该标签已关联商品，无法删除！");
        }
        //删除商品标签,假删除
        WelfareGoodsLabel welfareGoodsLabel = new WelfareGoodsLabel();
        welfareGoodsLabel.setId(labelId);
        welfareGoodsLabel.setStatus(CommonConstants.NO);
        welfareGoodsLabel.setUpdateTime(new Date());
        mapper.updateByPrimaryKeySelective(welfareGoodsLabel);
        return 1;
    }

    @Override
    public Integer updateGoodsLabelToTop(Integer labelId) {
        return mapper.updateGoodsLabelToTop(labelId);
    }

    @Override
    public Integer updateGoodsLabelCancelTop(Integer labelId) {
        return mapper.updateGoodsLabelCancelTop(labelId);
    }
}